import utilfuncs from '../../common/tools/utilfuncs.js';
var funcs = utilfuncs.UtilFuncs;

import globalConst from '../../common/globalConst.js';
var global = globalConst.GlobalConst;

class SingleArr {
    constructor(idx) {
        this.index = idx;
        this.list = [];
    }
}

class RectArraySvc {
    constructor() {
        this.tSrc2DstGraphMap = [];
    }

    array(dataManger, rowCount, rowGap, colCount, colGap) {
        let rst = null;
        dataManger.m_pOperStepService.record(1, []);
        if (this.initData(rowCount, colCount, dataManger)) {
            rst = this.arrayGraphs(rowCount, rowGap, colCount, colGap);
        }
        dataManger.m_pOperStepService.record(0, []);
        return rst;
    }

    initData(rowCount, colCount, dataManger) {
        let tSelGraphList = dataManger.getSelGraphs();
        if (tSelGraphList.length < 1) {
            return false;
        }
        for (let rowidx = 0; rowidx < rowCount; rowidx++) {
            for (let colIdx = 0; colIdx < colCount; colIdx++) {
                let index = rowidx * colCount + colIdx;
                if (index == 0) {
                    continue;
                }
                let singleArrInfo = new SingleArr(index);
                this.tSrc2DstGraphMap.push(singleArrInfo);
                for (let i = 0; i < tSelGraphList.length; i++) {
                    let srcGraph = tSelGraphList[i];
                    let dstGraph = dataManger.creatNewGraph4InnerSvc(srcGraph.getType());
                    if (dstGraph) {
                        let srcInfo = srcGraph.toInfo();
                        srcInfo.gid = dstGraph.m_gid;
                        srcInfo.rid = dstGraph.m_rid;
                        dstGraph.assignInfo(srcInfo);
                        let t = { src: srcGraph, dst: dstGraph };
                        singleArrInfo.list.push(t);
                    }
                }
            }
        }
        return true;
    }


    arrayGraphs(rowCount, rowGap, colCount, colGap) {
        console.log("arrayGraphs(rowCount, rowGap, colCount, colGap)***************");
        for (let n = 0; n < this.tSrc2DstGraphMap.length; n++) {
            let singleArrInfo = this.tSrc2DstGraphMap[n];
            let rowIdx = Math.floor(singleArrInfo.index / colCount);
            let colIdx = singleArrInfo.index % colCount;
            if (rowIdx == 0 && colIdx == 0) {
                continue;
            }
            for (let i = 0; i < singleArrInfo.list.length; i++) {
                let mirInfo = singleArrInfo.list[i];
                let srcGraph = mirInfo.src;
                let dstGraph = mirInfo.dst;
                let tx = colIdx * colGap;
                let ty = rowIdx * rowGap;

                let srcPosCopy = srcGraph.getPosCopy();
                srcPosCopy.offset(tx, ty);
                dstGraph.setPos(srcPosCopy);
                dstGraph.calScope(1);
                this.translateFill(srcGraph, dstGraph, tx, ty);
            }
        }
    }

    translateFill(srcGraph, dstGraph, tx, ty) {
        if (!funcs.isBaseUnit(srcGraph.getType())
            && srcGraph.m_graphType != LGXEnums.LGXGraphType.Path_type) {
            return;
        }

        let srcFill = srcGraph.getFillDef();
        if (srcFill.gradientInfo) {
            let transSrcStPt = { x: srcFill.gradientInfo.stPt.x + tx, y: srcFill.gradientInfo.stPt.y + ty };
            let transSrcEndPt = { x: srcFill.gradientInfo.endPt.x + tx, y: srcFill.gradientInfo.endPt.y + ty };

            let dstFill = dstGraph.getFillDef();
            dstFill.setStopPos("st", transSrcStPt);
            dstFill.setStopPos("end", transSrcEndPt);
        }
    }
}

export default { RectArraySvc }